/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package  com.edi.runtime.control
{
	import com.edi.runtime.Event.EDIBasicDataEvent;
	import com.edi.runtime.component.*;
	
	import flash.events.Event;
	
	import mx.containers.Box;
	import mx.containers.HBox;
	import mx.containers.HDividedBox;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.controls.DataGrid;
	/**
	 * 显示CheckBox列组件中每一行显示的ItemRenderer，用于在每一行前面添加一个CheckBox组件，
 	 * 用户选择后会将该行数据添加到CheckBoxColumn组件中的数组中保存
	 * 
	 */	
	public class CheckBoxItemRenderer extends CheckBox{
		
		private var currentData:Object; //保存当前一行值的对象
		public function CheckBoxItemRenderer(){
			super();
			
			this.addEventListener(Event.CHANGE,onClickCheckBox);
			this.addEventListener(EDIBasicDataEvent.Data_Delete_Render,cancelSelected,true);
			
			this.setStyle("horizontalCenter","0");
		}
		/**
		 * 选择框是否可见
		 * @param value
		 * 
		 */		
		public function set checkBoxVisible(value:Boolean):void
		{
			this.visible=value;
		}
		public function get checkBoxVisible():Boolean
		{
			return this.visible;
		}
		/**
		 * 复写数据属性
		 * @param value
		 * 
		 */		
		override public function set data(value:Object):void
		{
			if(value.selected=="true")
			{
				this.selected=true;
			}
			else
			{
				this.selected=false;
			}
			//this.selected = Boolean(value.selected);
			this.currentData = value; //保存整行的引用
		}
		/**
		 * 点击check box时，根据状况向selectedItems array中添加当前行的引用，或者从array中移除
		 * @param e
		 * 
		 */		  
		private function onClickCheckBox(e:Event):void{	
			var parentBasicData:EDIBasicDataWindow = null;
			try{
				parentBasicData = EDIBasicDataWindow((((((((this.owner as DataGrid).owner as VBox).owner as HBox).owner as EDIDataGrid).owner as Box).owner as HDividedBox).owner as VBox).owner);
			}catch(err:Error){
				
			}
			if(parentBasicData == null){
				return;
			}
			if(!parentBasicData.multiSelected && parentBasicData.returnArr.length>=1 && this.selected){
				EDIApplication.outputDebug("不可重新选择");
				//dgDataArr.selectRow.selected = "false";
				this.selected = false;
				Alert.yesLabel = "确定";
				EDIAlert.show("当前设置为单选！","温馨提示",Alert.YES,null,null,null,Alert.YES);
				return;
			}
			var dg:DataGrid = DataGrid(listData.owner);//获取DataGrid对象

			var column:CheckBoxColumn = dg.columns[listData.columnIndex];//获取整列的显示对象

			var dgDataArr:EDIDataTable = dg.dataProvider as EDIDataTable;
			var selectItems:Array = column.selectItems;
			if(dg.dataProvider is EDIDataTable)
			{
				
				(dg.dataProvider as EDIDataTable).cursorIndex=(dg.dataProvider as EDIDataTable).getItemIndex(currentData);
				dgDataArr.selectRow.selected=this.selected.toString();
			//this.currentData.dgSelected = this.selected;//根据是否选中的状态，更改数据源中选中的标记
			}
			var type:String;
			if(this.selected)
				type=EDIBasicDataEvent.Data_Insert;
			else
				type=EDIBasicDataEvent.Data_Delete;

			var evt:EDIBasicDataEvent=new EDIBasicDataEvent(type,true);
			evt.value=this.currentData;			
			this.owner.dispatchEvent(evt);
			
			if(this.selected){
				selectItems.push(this.currentData);
			}else{
				for(var i:int = 0; i<selectItems.length; i++){
					if(selectItems[i] == this.currentData){
						selectItems.splice(i,1);
					}
				}
			}
		}
		
		private function delItem(event:EDIBasicDataEvent):void{
			EDIApplication.outputDebug("CheckBoxItemRenderer-->EDIBasicDataEvent.Data_Delete_Render 捕捉到删除事件!!!!!!!!!!!");
		}
		
		private function cancelSelected(e:EDIBasicDataEvent):void{
			EDIApplication.outputDebug("获得了取消的句柄");
		}
		
	}
}